/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.colswe.solestac.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.colswe.solestac.entity.Permission;

/**
 *
 * @author Edison Parra
 * @version 1.0
 */
public class PermissionDao {

    /**Search al permissions given to a role.
     * @param rId The role given.
     * @param em EntityManager object to make the operation
     * @return A list of permissions for the role given.
     */
    public List<Permission> searchByRole(final long rId, final EntityManager em) {
        Query q = em.createQuery("SELECT p FROM Permission p WHERE p.ID IN "
                + "(SELECT u.PermissionID FROM permission_rol u WHERE u.rolID = :rId)").setParameter("rId", rId);
        return (List<Permission>) q.getResultList();
    }

    /**Create a permission.
     * @param p The new permission.
     * @param em EntityManager object to make the operation 
     */
    public void create(final Permission p, final EntityManager em) {
        em.persist(p);
    }

    /**Update a permission.
     * @param p The permission with the new information.
     * @param em EntityManager object to make the operation
     */
    public void update(final Permission p, final EntityManager em) {
        em.merge(p);
    }

    /**Delete a permission.
     * @param p The permission to delete.
     * @param em  EntityManager object to make the operation
     */
    public void delete(final Permission p, final EntityManager em) {
        em.remove(p);
    }
}
